{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.10 \u5e8f\u5217\u4e0a\u7d22\u5f15\u503c\u8fed\u4ee3\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u8fed\u4ee3\u4e00\u4e2a\u5e8f\u5217\u7684\u540c\u65f6\u8ddf\u8e2a\u6b63\u5728\u88ab\u5904\u7406\u7684\u5143\u7d20\u7d22\u5f15\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5185\u7f6e\u7684 enumerate() \u51fd\u6570\u53ef\u4ee5\u5f88\u597d\u7684\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "my_list = ['a', 'b', 'c']\nfor idx, val in enumerate(my_list):\n    print(idx, val)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u6309\u4f20\u7edf\u884c\u53f7\u8f93\u51fa(\u884c\u53f7\u4ece1\u5f00\u59cb)\uff0c\u4f60\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u5f00\u59cb\u53c2\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "my_list = ['a', 'b', 'c']\nfor idx, val in enumerate(my_list, 1):\n    print(idx, val)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u60c5\u51b5\u5728\u4f60\u904d\u5386\u6587\u4ef6\u65f6\u60f3\u5728\u9519\u8bef\u6d88\u606f\u4e2d\u4f7f\u7528\u884c\u53f7\u5b9a\u4f4d\u65f6\u5019\u975e\u5e38\u6709\u7528\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def parse_data(filename):\n    with open(filename, 'rt') as f:\n        for lineno, line in enumerate(f, 1):\n            fields = line.split()\n            try:\n                count = int(fields[1])\n                ...\n            except ValueError as e:\n                print('Line {}: Parse error: {}'.format(lineno, e))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "enumerate() \u5bf9\u4e8e\u8ddf\u8e2a\u67d0\u4e9b\u503c\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u4f4d\u7f6e\u662f\u5f88\u6709\u7528\u7684\u3002\n\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u4e00\u4e2a\u6587\u4ef6\u4e2d\u51fa\u73b0\u7684\u5355\u8bcd\u6620\u5c04\u5230\u5b83\u51fa\u73b0\u7684\u884c\u53f7\u4e0a\u53bb\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5229\u7528 enumerate() \u6765\u5b8c\u6210\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "word_summary = defaultdict(list)\n\nwith open('myfile.txt', 'r') as f:\n    lines = f.readlines()\n\nfor idx, line in enumerate(lines):\n    # Create a list of words in current line\n    words = [w.strip().lower() for w in line.split()]\n    for word in words:\n        word_summary[word].append(idx)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u5904\u7406\u5b8c\u6587\u4ef6\u540e\u6253\u5370 word_summary \uff0c\u4f1a\u53d1\u73b0\u5b83\u662f\u4e00\u4e2a\u5b57\u5178(\u51c6\u786e\u6765\u8bb2\u662f\u4e00\u4e2a defaultdict )\uff0c\n\u5bf9\u4e8e\u6bcf\u4e2a\u5355\u8bcd\u6709\u4e00\u4e2a key \uff0c\u6bcf\u4e2a key \u5bf9\u5e94\u7684\u503c\u662f\u4e00\u4e2a\u7531\u8fd9\u4e2a\u5355\u8bcd\u51fa\u73b0\u7684\u884c\u53f7\u7ec4\u6210\u7684\u5217\u8868\u3002\n\u5982\u679c\u67d0\u4e2a\u5355\u8bcd\u5728\u4e00\u884c\u4e2d\u51fa\u73b0\u8fc7\u4e24\u6b21\uff0c\u90a3\u4e48\u8fd9\u4e2a\u884c\u53f7\u4e5f\u4f1a\u51fa\u73b0\u4e24\u6b21\uff0c\n\u540c\u65f6\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u6587\u672c\u7684\u4e00\u4e2a\u7b80\u5355\u7edf\u8ba1\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u60f3\u989d\u5916\u5b9a\u4e49\u4e00\u4e2a\u8ba1\u6570\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u4f7f\u7528 enumerate() \u51fd\u6570\u4f1a\u66f4\u52a0\u7b80\u5355\u3002\u4f60\u53ef\u80fd\u4f1a\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "lineno = 1\nfor line in f:\n    # Process line\n    ...\n    lineno += 1"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f46\u662f\u5982\u679c\u4f7f\u7528 enumerate() \u51fd\u6570\u6765\u4ee3\u66ff\u5c31\u663e\u5f97\u66f4\u52a0\u4f18\u96c5\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for lineno, line in enumerate(f):\n    # Process line\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "enumerate() \u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a enumerate \u5bf9\u8c61\u5b9e\u4f8b\uff0c\n\u5b83\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u8fd4\u56de\u8fde\u7eed\u7684\u5305\u542b\u4e00\u4e2a\u8ba1\u6570\u548c\u4e00\u4e2a\u503c\u7684\u5143\u7ec4\uff0c\n\u5143\u7ec4\u4e2d\u7684\u503c\u901a\u8fc7\u5728\u4f20\u5165\u5e8f\u5217\u4e0a\u8c03\u7528 next() \u8fd4\u56de\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e00\u70b9\u53ef\u80fd\u5e76\u4e0d\u5f88\u91cd\u8981\uff0c\u4f46\u662f\u4e5f\u503c\u5f97\u6ce8\u610f\uff0c\n\u6709\u65f6\u5019\u5f53\u4f60\u5728\u4e00\u4e2a\u5df2\u7ecf\u89e3\u538b\u540e\u7684\u5143\u7ec4\u5e8f\u5217\u4e0a\u4f7f\u7528 enumerate() \u51fd\u6570\u65f6\u5f88\u5bb9\u6613\u8c03\u5165\u9677\u9631\u3002\n\u4f60\u5f97\u50cf\u4e0b\u9762\u6b63\u786e\u7684\u65b9\u5f0f\u8fd9\u6837\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "data = [ (1, 2), (3, 4), (5, 6), (7, 8) ]\n\n# Correct!\nfor n, (x, y) in enumerate(data):\n    ...\n# Error!\nfor n, x, y in enumerate(data):\n    ..."
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}